<?php namespace Home\Controller;
/**
 * 订单列表页
 * @author 武斌  <wubin.mail@foxmail.com>
 */
class OrderLController extends CommonController
{
	protected $db;
	public function __init()
	{
		if(!isset($_SESSION['username'])){
			go('Public/login');
		}		
	}
	/**
	 * 首页
	 */
	public function index()
	{
		$sid = Q('post.hiddenSid');
		if($sid)
		{
			$orderL = array();
			foreach ($sid as $k => $v) {
				$orderL['ord'][$v] = $_SESSION['cart']['goods'][$v];
			}
			$orderL['total'] = 0;
			$orderL['total_rows'] = 0;			
			foreach ($orderL['ord'] as $k => $v) {
				$orderL['total'] += $v['total'];
				$orderL['total_rows'] += $v['num'];
			}
			$_SESSION['ORDERL']= 	$orderL;
		}else
		{
			//View::error('请先选择要购买的商品');
		}
		//收货地址
		$useraddress = Db::table('useraddress')->get();		
		View::with('useraddress',$useraddress)->make(TEMPLATE . 'orderL.html');
	}
	/**
	 * 添加收货地址
	 */
	public function add()
	{
		$user_uid = $_SESSION['uid'];
		if(IS_POST){
			$userAddress = new \Home\Model\UserAddress;
			if(!$userAddress->addData($user_uid)){
				$this->error($userAddress->getError());
			}
		}
		go('index');		
	}
	/**
	 * 异步删除收货地址
	 */
	public function del(){		
		$uaid = Q('post.UAID',0,'intaval');
		$userAddress = new \Home\Model\UserAddress;
		$userAddress->where("uaid",$uaid)->delete();
		View::ajax(1);
	}	 
}
